
package com.craterhill.java.ems.dao.impl;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.craterhill.java.ems.common.beans.MarkingPeriods;
import com.craterhill.java.ems.dao.MarkingPeriodDao;

@Repository
public class MarkingPeriodDaoImpl extends AbstractDao<MarkingPeriods> implements MarkingPeriodDao {

	@Autowired
	public MarkingPeriodDaoImpl(final DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public List<MarkingPeriods> findBySession(int sessionId) {
		return super.findMultipleObject(FIND_MARKING_PERIOD_BY_SESSION, new MarkingPeriodsExtractor(), sessionId);
	}

	@Override
	public boolean save(final int sessionId, final List<MarkingPeriods> markingPeriodList) {
		return super.batchOperation(SAVE_MARKING_PERIOD, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				MarkingPeriods markingPeriods = markingPeriodList.get(position);
				preparedStatement.setInt(1, sessionId);
				preparedStatement.setString(2, markingPeriods.getMarkingPeriodTitle());
				preparedStatement.setDate(3, new Date(markingPeriods.getMarkingPeriodStartDate().getTime()));
				preparedStatement.setDate(4, new Date(markingPeriods.getMarkingPeriodEndDate().getTime()));
				preparedStatement.setInt(5, markingPeriods.getCreatedBy());
			}

			@Override
			public int getBatchSize() {
				return markingPeriodList.size();
			}
		});
	}

	@Override
	public boolean remove(final Integer... sessionIds) {
		return super.batchOperation(DELETE_MARKING_PERIOD, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				preparedStatement.setInt(1, sessionIds[position]);
			}

			@Override
			public int getBatchSize() {
				return sessionIds.length;
			}
		});
	}

	@Override
	public MarkingPeriods find(int recordId) {
		return null;
	}

	@Override
	public List<MarkingPeriods> findAll() {
		return null;
	}

	@Override
	public int save(MarkingPeriods t) {
		return 0;
	}

	@Override
	public boolean update(MarkingPeriods t) {
		return false;
	}
}
